package com.physics.mps.module.comsol.builder.mat;

import com.physics.mps.module.comsol.builder.Builder;
import com.physics.mps.module.comsol.vo.MaterialVo;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @author mcsoft
 * @date 2023/10/16 10:42
 * @description comsol java 材料脚本构建
 */
public class MaterialBuilder {

    private final Builder builder;


    private static final Map<String, String> GROUP_MAP ;

    private final Set<String> GROUP_SET ;




    static {
        GROUP_MAP = new HashMap<>();

        GROUP_MAP.put("Enu", "Young's_modulus_and_Poisson's_ratio");
    }


    public MaterialBuilder(Builder builder) {
        this.builder = builder;
        this.GROUP_SET = new HashSet<>();
    }


    private static final String SET_MAT_PROPERTY = "%s.propertyGroup(\"%s\").set(\"%s\", \"%s\");";


    public void build(String geomTag, List<MaterialVo> vos) {
        builder.echo("Material");

        if (CollectionUtils.isEmpty(vos)) {
            return;
        }
        for (MaterialVo vo : vos) {
            builder.change(1, 0);
            build(geomTag, vo);
        }
    }

    public void build(String geomTag, MaterialVo vo) {
        String mat = builder.uniqueTag("material");
        createMaterial(mat);
        setProperty(mat, vo.getProperties());

        builder.change(1, 2);
        if (vo.isSelectAll()) {
            builder.append(mat);
            builder.append(".selection().all();");
            return;
        }
        String ballSelect = builder.createBallSelect(vo.getEntityDim(), vo.getCoordinate());
        builder.change(1, 2);
        builder.append(mat);
        builder.append(".selection().named(\"");
        builder.append(geomTag);
        builder.append("_");
        builder.append(ballSelect);
        builder.append("\");");

    }


    /**
     * 创建材料
     * @param mat
     */
    private void createMaterial(String mat) {
        builder.change(1, 2);
        String matTag = builder.uniqueTag("mat");
        builder.append("Material ");
        builder.append(mat);
        builder.append(" = ");
        builder.append("comp.material().create(");
        builder.value(matTag);
        builder.append(", \"Common\");");
    }


    /**
     * 设置材料属性
     * @param mat
     * @param properties
     */
    private void setProperty(String mat, Map<String, String> properties){
        if(CollectionUtils.isEmpty(properties)) {
            return;
        }
        properties.forEach((key, value) -> {
            String group = "def";
            String name = key;
            if (key.indexOf("_") > 0) {
                String[] splits = key.split("_");
                group = splits[0];
                createMaterialGroup(mat, group);
                name = splits[1];
            }
            builder.change(1, 2);
            builder.append(String.format(SET_MAT_PROPERTY, mat, group, name, value));
        });
    }


    /**
     * 创建材料组
     * @param mat
     * @param groupKey
     */
    private void createMaterialGroup(String mat, String groupKey) {
        if (GROUP_SET.contains(mat + groupKey)) {
            return;
        }
        String value = GROUP_MAP.get(groupKey);

        builder.change(1, 2);
        builder.append(mat);
        builder.append(".propertyGroup().create(");
        builder.value(groupKey);
        builder.append(", ");
        builder.value(value);
        builder.append(");");
        GROUP_SET.add(mat + groupKey);
    }


}
